﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UNSWThink
{
   /// <summary>
   /// Windows form that displays a list of the responses
   /// </summary>
   public partial class ViewResponsesForm : Form
   {
      private bool mode;
      private int questionID;
      private DataTable responses;
      private DataTable relatedResponses;
      private int offset;

      /// <summary>
      /// Default constructor
      /// </summary>
      /// <param name="mode">
      /// Mode which corresponds to how the formfunctions
      /// RESPONSE_MODE = form displays responses
      /// ENQUIRY_MODE = form displays enquiries
      /// </param>
      /// <param name="questionID">
      /// ID of the question
      /// </param>
      /// <param name="responses">
      /// Data table containing all of the responses related to the selected class
      /// </param>
      public ViewResponsesForm(bool mode, int questionID, DataTable responses)
      {
         InitializeComponent();

         this.mode = mode;
         this.questionID = questionID;
         this.responses = responses;
         this.offset = 0;

         relatedResponses = new DataTable();
      }

      #region WINDOWS FORM EVENTS

      private void ViewResponsesForm_Load(object sender, EventArgs e)
      {
         // check responses contain expected table column name precondition
         if (HelperMethods.CheckColumnsExists(responses, Defs.RESPONSE_COLUMN_NAMES) ||
             HelperMethods.CheckColumnsExists(responses, Defs.ENQUIRY_COLUMN_NAMES))
         {
            // get all column names and add to new data table
            for (int i = 0; i < responses.Columns.Count; i++)
            {
               DataColumn newColumn = new DataColumn(responses.Columns[i].ColumnName, 
                  responses.Columns[i].DataType);
               newColumn.ReadOnly = true;
               relatedResponses.Columns.Add(newColumn);
            }

            // find and add related responses to the new data table
            for (int i = 0; i < responses.Rows.Count; i++)
            {
               if (mode == Defs.RESPONSE_MODE)
               {
                  if ((int)responses.Rows[i]["question"] == questionID)
                  {
                     relatedResponses.Rows.Add(responses.Rows[i].ItemArray);
                  }
               }
               else
               {
                  relatedResponses.Rows.Add(responses.Rows[i].ItemArray);
               }
            }

            // set offset to the size of the original table so that when updating the form
            // the program does not start from the beginning re-retrieving already retrieved responses
            offset = responses.Rows.Count;

            // initalise various properties of the response data grid view control
            dataGridViewResponse.DataSource = relatedResponses;

            dataGridViewResponse.Columns["id"].Visible = false;
            dataGridViewResponse.Columns["description"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;

            int totalWidth = 0;
            if (mode == Defs.RESPONSE_MODE)
            {
               dataGridViewResponse.Columns["question"].Visible = false;
               dataGridViewResponse.Columns["type"].Visible = false;

               totalWidth = dataGridViewResponse.Columns["description"].Width;
            }
            else
            {
               dataGridViewResponse.Columns["class"].Visible = false;
               dataGridViewResponse.Columns["time"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCellsExceptHeader;

               totalWidth = dataGridViewResponse.Columns["description"].Width + 
                  dataGridViewResponse.Columns["time"].Width;
            }

            if (totalWidth < dataGridViewResponse.Width)
            {
               dataGridViewResponse.Columns["description"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            }
            else
            {
               dataGridViewResponse.Columns["description"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            }
         }
      }

      #endregion

      #region HELPER METHODS

      /// <summary>
      /// Updates the form with the latest responses
      /// </summary>
      public void UpdateList()
      {
         if (HelperMethods.CheckColumnsExists(relatedResponses, Defs.RESPONSE_COLUMN_NAMES))
         {
            // starts loop from offset so that the entire data table is not searched
            for (int i = offset; i < responses.Rows.Count; i++)
            {
               if (mode == Defs.RESPONSE_MODE)
               {
                  if ((int)responses.Rows[i]["question"] == questionID)
                  {
                     relatedResponses.Rows.Add(responses.Rows[i].ItemArray);
                  }
               }
               else
               {
                  relatedResponses.Rows.Add(responses.Rows[i].ItemArray);
               }
            }

            // set new offset
            offset = responses.Rows.Count;

            // reset the response data grid view with the updated data table
            dataGridViewResponse.DataSource = relatedResponses;

            if (dataGridViewResponse.Columns["description"].AutoSizeMode ==
               DataGridViewAutoSizeColumnMode.Fill)
            {
               dataGridViewResponse.Columns["description"].AutoSizeMode =
                  DataGridViewAutoSizeColumnMode.AllCells;

               int totalWidth = 0;
               if (mode == Defs.RESPONSE_MODE)
               {
                  totalWidth = dataGridViewResponse.Columns["description"].Width;
               }
               else
               {
                  totalWidth = dataGridViewResponse.Columns["description"].Width +
                     dataGridViewResponse.Columns["time"].Width;
               }

               if (totalWidth < dataGridViewResponse.Width)
               {
                  dataGridViewResponse.Columns["description"].AutoSizeMode =
                     DataGridViewAutoSizeColumnMode.Fill;
               }
               else
               {
                  dataGridViewResponse.Columns["description"].AutoSizeMode =
                     DataGridViewAutoSizeColumnMode.AllCells;
               }
            }
         }
         else
         {
            // TODO: fix?!?!?!?
            Console.WriteLine("description doesn't exist");
            relatedResponses.Columns.Add("desciption");
            this.Activate();
         }
      }

      #endregion

   }
}
